package com.base.server.sys.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.base.server.sys.pojo.SysDictType;
import com.base.server.sys.service.SysDictTypeService;
import com.monitor.api.ApiInfo;
import com.monitor.api.ApiParam;
import com.monitor.api.ApiRes;
import com.system.comm.model.Orderby;
import com.system.comm.utils.FrameJsonUtil;
import com.system.comm.utils.FrameStringUtil;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;

/**
 * 字典分类的Controller
 * @author yuejing
 * @date 2019-08-05 16:30:02
 * @version V1.0.0
 */
@RestController
public class SysDictTypeController {

	private static final Logger LOGGER = LoggerFactory.getLogger(SysDictTypeController.class);

	@Autowired
	private SysDictTypeService sysDictTypeService;

	@RequestMapping(name = "字典分类-新增或修改", value = "/sysDictType/saveOrUpdate")
	@ApiInfo(params = {
			@ApiParam(name="编码", code="code", value="GENDER"),
			@ApiParam(name="名称", code="name", value="性别"),
			@ApiParam(name="上级编码", code="parentCode", value="0"),
			@ApiParam(name="备注", code="remark", value=""),
			@ApiParam(name="当前登录人ID", code="currentUserId", value="1"),
	}, response = {
			@ApiRes(name="响应码[0成功、-1失败]", code="code", clazz=String.class, value="0"),
			@ApiRes(name="响应消息", code="message", clazz=String.class, value="success"),
			@ApiRes(name="主体内容", code="body", clazz=Object.class, value=""),
	})
	public ResponseFrame saveOrUpdate(SysDictType sysDictType) {
		try {
			sysDictType.setCreateUserId(sysDictType.getCurrentUserId());
			ResponseFrame frame = sysDictTypeService.saveOrUpdate(sysDictType);
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new ResponseFrame(ResponseCode.SERVER_ERROR);
		}
	}
	
	@RequestMapping(name = "字典分类-获取详细信息", value = "/sysDictType/get")
	@ApiInfo(params = {
			@ApiParam(name="code", code="code", value="GENDER"),
	}, response = {
			@ApiRes(name="响应码[0成功、-1失败]", code="code", clazz=String.class, value="0"),
			@ApiRes(name="响应消息", code="message", clazz=String.class, value="success"),
			@ApiRes(name="主体内容", code="body", clazz=Object.class, value=""),
			
	})
	public ResponseFrame get(String code) {
		try {
			ResponseFrame frame = new ResponseFrame();
			frame.setBody(sysDictTypeService.get(code));
			frame.setCode(ResponseCode.SUCC.getCode());
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new ResponseFrame(ResponseCode.SERVER_ERROR);
		}
	}

	@RequestMapping(name = "字典分类-分页查询信息", value = "/sysDictType/pageQuery")
	@ApiInfo(params = {
			@ApiParam(name="页面", code="page", value="1"),
			@ApiParam(name="每页大小", code="size", value="10"),
			@ApiParam(name="排序[{\"property\": \"createTime\", \"type\":\"desc\", \"order\":1}]", code="orderby", value="", required=false),
	}, response = {
			@ApiRes(name="响应码[0成功、-1失败]", code="code", clazz=String.class, value="0"),
			@ApiRes(name="响应消息", code="message", clazz=String.class, value="success"),
			@ApiRes(name="主体内容", code="body", clazz=Object.class, value=""),
			@ApiRes(name="当前页码", code="page", pCode="body", clazz=Integer.class, value="1"),
			@ApiRes(name="每页大小", code="size", pCode="body", clazz=Integer.class, value="10"),
			@ApiRes(name="总页数", code="totalPage", pCode="body", clazz=Integer.class, value="5"),
			@ApiRes(name="总记录数", code="total", pCode="body", clazz=Integer.class, value="36"),
			@ApiRes(name="数据集合", code="rows", pCode="body", clazz=List.class, value=""),
			@ApiRes(name="编码", code="code", pCode="rows", value=""),
			@ApiRes(name="名称", code="name", pCode="rows", value=""),
			@ApiRes(name="上级编码", code="parentCode", pCode="rows", value=""),
			@ApiRes(name="备注", code="remark", pCode="rows", value=""),
			@ApiRes(name="添加人", code="createUserId", pCode="rows", value=""),
			@ApiRes(name="添加时间", code="createTime", pCode="rows", value=""),
	})
	public ResponseFrame pageQuery(SysDictType sysDictType, String orderby) {
		try {
			if(FrameStringUtil.isNotEmpty(orderby)) {
				List<Orderby> orderbys = FrameJsonUtil.toList(orderby, Orderby.class);
				sysDictType.setOrderbys(orderbys);
			}
			ResponseFrame frame = sysDictTypeService.pageQuery(sysDictType);
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new ResponseFrame(ResponseCode.SERVER_ERROR);
		}
	}

	@RequestMapping(name = "字典分类-根据主键删除", value = "/sysDictType/delete")
	@ApiInfo(params = {
			@ApiParam(name="code", code="code", value=""),
	}, response = {
			@ApiRes(name="响应码[0成功、-1失败]", code="code", clazz=String.class, value="0"),
			@ApiRes(name="响应消息", code="message", clazz=String.class, value="success"),
			@ApiRes(name="主体内容", code="body", clazz=Object.class, value=""),
	})
	public ResponseFrame delete(String code) {
		try {
			ResponseFrame frame = sysDictTypeService.delete(code);
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new ResponseFrame(ResponseCode.SERVER_ERROR);
		}
	}
}